package test.jmx;

import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

public class JmxServer {

	public static void main(String[] args) throws Exception {
		MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
		ObjectName name = new ObjectName("com.mochasoft.bsm:name=HelloMBean");
		mbeanServer.registerMBean(new Hello(), name);
		
		LocateRegistry.createRegistry(8888);
		JMXServiceURL serviceURL = new JMXServiceURL("service:jmx:rmi://localhost/jndi/rmi://localhost:8888/bsm");
		JMXConnectorServer connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(serviceURL, null, mbeanServer);
		connectorServer.start();
		//本地调用不需要启动jmx连接服务器
		mbeanServer.invoke(name, "printHello", new Object[] { "lucy" }, new String[] { String.class.getName() });
	}

}
